package org.vy.dal.dao.lookup;

import java.util.List;

import org.vy.dal.QueryManager;

public class VyLookupDeliveryModeDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE DELIVERY_MODE_CODE = m_DeliveryModeCode";
	private static final String FIND_BY_PARAM = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String FIND_ALL_ACTIVE = "SELECT * FROM <TABLE> WHERE 1 = 1 AND IS_ACTIVE = TRUE";
	private static final String UPDATE = "UPDATE <TABLE> SET DELIVERY_MODE_NAME = m_DeliveryModeName, IS_ACTIVE = m_IsActive, LAST_UPDATED_BY = m_LastUpdtBy, LAST_UPDATED_ON = m_LastUpdtOn WHERE DELIVERY_MODE_CODE = m_DeliveryModeCode";
	private static final String INSERT = "INSERT INTO <TABLE> (DELIVERY_MODE_CODE,DELIVERY_MODE_NAME,IS_ACTIVE,LAST_UPDATED_BY,LAST_UPDATED_ON) VALUES(m_DeliveryModeCode,m_DeliveryModeName,m_IsActive,m_LastUpdtBy,m_LastUpdtOn)";
	private static final String DELETE = "UPDATE <TABLE> SET IS_ACTIVE = FALSE WHERE DELIVERY_MODE_CODE = m_DeliveryModeCode";

	public static VyLookupDeliveryModeDO findByPK(String key) throws Exception {
		VyLookupDeliveryModeDO dto = new VyLookupDeliveryModeDO();
		dto.setDeliveryModeCode(key);
		dto = (VyLookupDeliveryModeDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static List<VyLookupDeliveryModeDO> findAllActive() throws Exception {
		VyLookupDeliveryModeDO dto = new VyLookupDeliveryModeDO();
		return (List<VyLookupDeliveryModeDO>) QueryManager.executeListQuery(dto, FIND_ALL_ACTIVE);
	}
	
	public static VyLookupDeliveryModeDO insert(VyLookupDeliveryModeDO dto) throws Exception {

		dto = (VyLookupDeliveryModeDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyLookupDeliveryModeDO update(VyLookupDeliveryModeDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyLookupDeliveryModeDO delete(VyLookupDeliveryModeDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, DELETE);
		if (i > 0)
			return dto;
		else
			return null;
	}
	@SuppressWarnings("unchecked")
	public static List<VyLookupDeliveryModeDO> findByParam(VyLookupDeliveryModeDO dto)
			throws Exception {
		StringBuilder sb = new StringBuilder(FIND_BY_PARAM);
		if (dto.getDeliveryModeCode() != null) {
			sb.append(" AND DELIVERY_MODE_CODE LIKE '%" + dto.getDeliveryModeCode()
					+ "%' ");
		}
		if (dto.getDeliveryModeName() != null) {
			sb.append(" AND DELIVERY_MODE_NAME LIKE '%" + dto.getDeliveryModeName()
					+ "%' ");
		}
		if (dto.getIsActive() != null) {
			sb.append(" AND IS_ACTIVE LIKE '%" + dto.getIsActive()
					+ "%' ");
		}
		if (dto.getLastUpdtBy() != null) {
			sb.append(" AND LAST_UPDATED_BY LIKE '%" + dto.getLastUpdtBy()
					+ "%' ");
		}
		if (dto.getLastUpdtOn() != null) {
			sb.append(" AND LAST_UPDATED_ON LIKE '%" + dto.getLastUpdtOn()
					+ "%' ");
		}
		
		System.out.println(sb.toString());
		return (List<VyLookupDeliveryModeDO>) QueryManager.executeListQuery(dto, sb
				.toString());
	}

}
